package com.yx.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.plugins.Page;
import com.yx.common.annotation.Log;
import com.yx.common.annotation.LoginUser2;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.modules.api.entity.TbInterview;
import com.yx.modules.api.service.TbInterviewService;
import com.yx.system.entity.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 访谈
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-06-14 15:00:09
 */
@RestController
@RequestMapping("sys/interview")
public class SysInterviewController2 {
	@Autowired
	private TbInterviewService interviewService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:interview:list")
	public R list(@RequestParam Map<String, Object> map){
		Query query = new Query(map);
		Page<TbInterview> pageUtil = new Page<TbInterview>(query.getPage(), query.getLimit());
		Page<TbInterview> page = interviewService.queryPageList(pageUtil, query);
		return R.ok().put("page", page);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/findById/{id}")
	@RequiresPermissions("sys:interview:findById")
	public R info(@PathVariable("id") Integer id){
		TbInterview interview = interviewService.queryObject(id);
		
		return R.ok().put("interview", interview);
	}

	/**
	 * 新增或修改
	 *
	 * @return
	 */
	@Log("添加或修改访谈")
	@PostMapping("/saveOrUpdate")
	@RequiresPermissions("sys:interview:saveOrUpdate")
	public R saveOrUpdate(@RequestBody TbInterview interview, @LoginUser2 SysUser sysUser) {
		Assert.isBlank(interview.getHeadline(),"书名不能为空");
		Assert.isBlank(interview.getContent(),"图书封面不能为空");
		Assert.isNull(interview.getPicture(),"图书所属分类不能为空");
		if (interview.getId() == null) {
			interview.setDate(new Date());
			interview.setCommentCount(0);
			interview.setClickCount(0);
			interview.setLikeCount(0);
			interviewService.save(interview);
		} else {
			interview.setDate(new Date());
			interviewService.update(interview);
		}
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:interview:delete")
	public R delete(@RequestBody Integer[] ids){
		interviewService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
